package com.mycloud.sample.resource;

import com.mycloud.sample.entity.User;
import com.mycloud.sample.service.UserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Random;
import java.util.UUID;

import lombok.extern.slf4j.Slf4j;

/**
 * @author : yinlin
 * @version : 1.0
 * @date : 2018/9/26 下午6:59
 * @Description :
 * @Copyright : Copyright (c) 2018
 * @Company : KeRuYun Technology(Beijing) Chengdu Co. Ltd.
 * @link : com.mycloud.sample.resource.UserResource
 */
@RestController
@Slf4j
public class UserResource {

    @Autowired
    private UserService userService;

    @RequestMapping(value = "user/save")
    public User save() {
        User user = new User();
        user.setAge(Long.valueOf(new Random().nextInt(100)));
        user.setName(UUID.randomUUID().toString().replaceAll("-", "").toUpperCase());
        return userService.save(user);
    }

    @RequestMapping(value = "users")
    public List<User> users() {
        return userService.findAll();
    }

    @RequestMapping(value = "user/{id}")
    public boolean delete(@PathVariable("id") Long id) {
        userService.delete(id);
        return true;
    }
}
